<?php
namespace app\index\controller;
use think\Db;

class Index extends Base
{
    public function index() //头部和左侧页面
    {
        //session_start();
        //sdcz
        $this->baseindex();
        $id = $_SESSION['id'];
        //$sessionid=$_SESSION['sessionid'];
        $jurisdictions = '';
        $array = '';
        $jurisdiction_keyid = '';
        $jurisdiction_pids = '';
        //$jurisdiction_pidss='';
        //取出员工拥有的角色ID
        $role_id = Db::table('oa_user_role u,oa_role r')->field('u.role_id')
            ->where('r.id=u.role_id  and u.state=1 and u.user_id=' . $id. ' and ( r.state=1 or  r.state=2)')->select();
        //取出对应角色拥有的权限
        //dump($role_id);die;
        foreach ($role_id as $k => $v) {
            $jurisdiction_id = Db::table('oa_role_jurisdiction')->field('jurisdiction_id')
                ->where('state=1 and role_id=' . $v['role_id'])->select();
            //dump($jurisdiction_id);die;
            foreach ($jurisdiction_id as $key => $value) {
                $array[] = $value['jurisdiction_id'];
            }
        }
        //dump($array);
        $temp_a = array_unique($array);
        $temp =array_values($temp_a);
        //dump($temp_b);die;
        sort($temp);
         //dump($temp_b);die;
        for ($i = 0; $i < count($temp); $i++) {
            $jurisdiction = Db::table('oa_jurisdiction')->field('id,pid,jurisdiction,url')
                ->where('pid=0 and id=' . $temp[$i])->find();
            if (!empty($jurisdiction)) {
                $jurisdictions[] = $jurisdiction;
            } else {
                $jurisdiction_keyid[] = $temp[$i];
            }
        }
        //dump($jurisdictions);
        //dump($jurisdiction_keyid);

        foreach ($jurisdictions as $kk => $vv) {
            for ($j = 0; $j < count($jurisdiction_keyid); $j++) {
                $jurisdiction_pid = Db::table('oa_jurisdiction')->field('id,pid,jurisdiction,url')
                    ->where('id=' . $jurisdiction_keyid[$j] . ' and pid=' . $vv['id'])->find();
                if (!empty($jurisdiction_pid)) {
                    $jurisdiction_pids[] = $jurisdiction_pid;
                }
            }
            $jurisdictions[$kk]['list'] = $jurisdiction_pids;
            $jurisdiction_pids = '';
        }
        //dump($jurisdictions);die;
        $this->assign('id', $id);
        //$this->assign('sessionid', $sessionid);
        $this->assign('nav', $jurisdictions);
        return $this->fetch();
    }

}                   